<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>比较并交换值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="memcached.appendbykey.html">« Memcached::appendByKey</a></li>
      <li style="float: right;"><a href="memcached.casbykey.html">Memcached::casByKey »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.memcached.html">Memcached</a></li>
    <li>比较并交换值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="memcached.cas" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Memcached::cas</h1>
  <p class="verinfo">(PECL memcached &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">Memcached::cas</span> &mdash; <span class="dc-title">比较并交换值</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-memcached.cas-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Memcached::cas</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">float</span> <code class="parameter">$cas_token</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$expiration</code><span class="initializer"> = ?</span></span><br>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>Memcached::cas()</strong></span>执行一个“检查并设置”的操作，因此，它仅在当前客户端最后一次取值后，该key
   对应的值没有被其他客户端修改的情况下， 才能够将值写入。检查是通过<code class="parameter">cas_token</code>参数进行的， 
   这个参数是Memcach指定给已经存在的元素的一个唯一的64位值， 怎样获取这个值请查看<span class="methodname"><strong>Memcached::get*()</strong></span>
   系列方法的文档。注意：这个值作为double类型是因为PHP的整型空间限制。
  </p>
  <p class="para">
  译注：这是Memcached扩展比Memcache扩展一个非常重要的优势， 在这样一个系统级（Memcache自身提供）的冲突检测机制（乐观锁）下，
  我们才能保证高并发下的数据安全。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-memcached.cas-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">cas_token</code></dt>

     <dd>

      <p class="para">
      与已存在元素关联的唯一的值，由Memcache生成。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">key</code></dt>

     <dd>

      <p class="para">
       用于存储值的键名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       存储的值。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">expiration</code></dt>

     <dd>

      <p class="para">
       到期时间，默认为 0。 更多信息请参见<a href="memcached.expiration.html" class="link">到期时间</a>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-memcached.cas-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
   如果在元素尝试存储时发现在本客户端最后一次获取后被其他客户端修改，<span class="methodname"><a href="memcached.getresultcode.html" class="methodname">Memcached::getResultCode()</a></span>
   将返回<strong><code>Memcached::RES_DATA_EXISTS</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-memcached.cas-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4383">
    <p><strong>示例 #1 <span class="function"><strong>Memcached::cas()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /><br />do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;获取ip列表以及它的标记&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ips&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'ip_block'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$cas</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;如果列表不存在，&nbsp;创建并进行一个原子添加（如果其他客户端已经添加，&nbsp;这里就返回false）*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getResultCode</span><span style="color: #007700">()&nbsp;==&nbsp;</span><span style="color: #0000BB">Memcached</span><span style="color: #007700">::</span><span style="color: #0000BB">RES_NOTFOUND</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ips&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'ip_block'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ips</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;其他情况下，添加ip到列表中，&nbsp;并以cas方式去存储，&nbsp;这样当其他客户端修改过，&nbsp;则返回false&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;else&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ips</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cas</span><span style="color: #007700">(</span><span style="color: #0000BB">$cas</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ip_block'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ips</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getResultCode</span><span style="color: #007700">()&nbsp;!=&nbsp;</span><span style="color: #0000BB">Memcached</span><span style="color: #007700">::</span><span style="color: #0000BB">RES_SUCCESS</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-memcached.cas-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="memcached.casbykey.html" class="methodname" rel="rdfs-seeAlso">Memcached::casByKey()</a> - 在指定服务器上比较并交换值</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>